﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Common.Util;
using DevExpress.Utils.Menu;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using Log.Controller;
using Tabber.Model;
using Tabber.Util;

namespace Tabber.Controls
{
    public partial class LanguageSelector : DevExpress.XtraEditors.XtraUserControl
    {
        /// <summary>
        /// Ham delegate duoc goi sau khi su kien selected index change cua combobox language
        /// </summary>
        /// <returns></returns>
        public delegate void AfterLanguageChanged();

        /// <summary>
        /// Ham callback duoc gan boi parent form
        /// </summary>
        public AfterLanguageChanged ReSetText;      

        public LanguageSelector()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Su kien control duoc load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LanguageSelector_Load(object sender, EventArgs e)
        {
            try
            {
                SetText();
            }
            catch (Exception ex)
            {
                LogController.WriteLog(System.Runtime.InteropServices.Marshal.GetExceptionCode(), ex, Network.GetLocalIp());
            }
        }

        /// <summary>
        /// Su kien gia tri combobox thay doi thi set lai ngon ngu
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cboLanguage_SelectedValueChanged(object sender, EventArgs e)
        {
            var cbo = (ImageComboBoxEdit) sender;
            TabberCommon.SetLanguage(((ImageComboBoxItem)cbo.SelectedItem).Value.ToString());
            SetText();

            // Neu ham ReSetText duoc gan trong parent form
            if (ReSetText != null) ReSetText();
        }

        /// <summary>
        /// Ham set text theo ngon ngu
        /// </summary>
        private void SetText()
        {
            cboLanguage.Properties.Items.Clear();
            cboLanguage.Properties.Items.Add(new ImageComboBoxItem(TabberCommon.GetString(Localization.Vietnamese), Localization.Vietnamese));
            cboLanguage.Properties.Items.Add(new ImageComboBoxItem(TabberCommon.GetString(Localization.English), Localization.English));
            cboLanguage.SelectedItem = cboLanguage.Properties.Items.GetItem(TabberCommon.CurrentLanguage);
        }

    }
}
